{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x7f3754907f30>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.autograd import Variable\n",
    "torch.manual_seed(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------input------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.4461 -1.7313  0.3699 -0.3564\n",
      " -0.3377 -0.1462  0.2211  0.2101\n",
      " -0.6409 -0.0282 -0.5603 -0.0125\n",
      "  0.4262 -1.0960  0.8607 -1.9942\n",
      " -1.3192 -0.4212  1.4077 -0.3445\n",
      "\n",
      "(1 ,.,.) = \n",
      " -0.1371 -0.4603 -0.7347 -0.1396\n",
      "  0.2408 -0.5533 -2.0483  1.1918\n",
      "  0.8283  0.1775 -2.0114 -0.8362\n",
      "  0.6369  1.0047 -0.0170 -1.8938\n",
      "  0.6460 -0.5101  0.9348 -0.7416\n",
      "[torch.FloatTensor of size 2x5x4]\n",
      "\n",
      "-------------h0--------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.5200 -2.6718  1.1100 -1.4660  1.2769 -0.5401  1.8059 -0.5611\n",
      "  1.1396 -0.2842 -1.3839 -2.2763  0.6903 -0.7146 -0.6641  0.1318\n",
      "[torch.FloatTensor of size 1x2x8]\n",
      "\n",
      "-----------output------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.8269  0.5821 -0.3771 -0.6001 -0.2826  0.4665 -0.7994  0.6679\n",
      " -0.1299  0.5016 -0.5509 -0.1767 -0.3701  0.4761  0.5418 -0.3435\n",
      " -0.3707  0.2710 -0.0693 -0.2781  0.1795  0.3329 -0.3951 -0.3599\n",
      " -0.7371  0.7549 -0.7227 -0.7748  0.0031 -0.5893 -0.1918  0.0820\n",
      " -0.3120  0.0304 -0.5284 -0.7872 -0.5912  0.6447  0.6986  0.2810\n",
      "\n",
      "(1 ,.,.) = \n",
      "  0.7591  0.7558 -0.0110 -0.7744  0.0326  0.5142  0.4415  0.8459\n",
      "  0.0767  0.1927  0.5806  0.5193  0.7514  0.3892 -0.4128 -0.6041\n",
      " -0.4266  0.8647 -0.1858  0.0777  0.7475 -0.4314 -0.6181  0.0252\n",
      " -0.2266  0.8464 -0.8077 -0.5185  0.6583 -0.1323  0.6981  0.4534\n",
      " -0.5025  0.0468 -0.6007 -0.5744  0.4496  0.2915  0.5066 -0.2065\n",
      "[torch.FloatTensor of size 2x5x8]\n",
      "\n",
      "-------------hn--------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.3120  0.0304 -0.5284 -0.7872 -0.5912  0.6447  0.6986  0.2810\n",
      " -0.5025  0.0468 -0.6007 -0.5744  0.4496  0.2915  0.5066 -0.2065\n",
      "[torch.FloatTensor of size 1x2x8]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "rnn = nn.RNN(4, 8, 1, batch_first=True)\n",
    "input = Variable(torch.randn(2, 5, 4))\n",
    "h0 = Variable(torch.randn(1, 2, 8))\n",
    "output, hn = rnn(input, h0)\n",
    "print(\"------------input------------\")\n",
    "print(input)\n",
    "print(\"-------------h0--------------\")\n",
    "print(h0)\n",
    "print(\"-----------output------------\")\n",
    "print(output)\n",
    "print(\"-------------hn--------------\")\n",
    "print(hn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import RNNs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------test_input------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.0691 -0.5530  2.2385 -1.6356\n",
      " -0.6769 -0.0516 -1.0682 -0.1689\n",
      "  0.5377 -0.1096 -1.0031  0.3782\n",
      " -0.0508  0.2026 -0.2102  0.4983\n",
      "  0.7139  0.1134  1.4180 -0.6722\n",
      "\n",
      "(1 ,.,.) = \n",
      "  0.0133  2.2589 -0.4735 -0.8769\n",
      "  0.3162  0.3461  1.5042 -0.3223\n",
      "  0.9323 -0.3554 -1.5606 -0.4223\n",
      "  0.2478  0.4774 -0.1482  0.7348\n",
      "  0.3875  1.2409  0.8882  0.5417\n",
      "[torch.FloatTensor of size 2x5x4]\n",
      "\n",
      "------------test_output------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.0000  0.2342  0.2515  0.1703  0.1553  0.0000  0.1942  0.0000\n",
      "  0.0000  0.0141  0.0000  0.0000  0.5008  0.0000  0.0000  0.0000\n",
      "  0.0000  0.1881  0.0000  0.0000  0.6279  0.2305  0.0000  0.0000\n",
      "  0.0000  0.2075  0.6262  0.3200  0.0000  0.0660  0.4977  0.0000\n",
      "  0.0000  0.4289  0.0619  0.0000  0.5793  0.0000  0.5234  0.0000\n",
      "\n",
      "(1 ,.,.) = \n",
      "  0.0241  0.1826  0.0379  0.0000  0.3634  0.1607  0.0000  0.0000\n",
      "  0.4287  0.0000  0.0000  0.0178  0.7399  0.1275  0.0000  0.0000\n",
      "  0.0000  0.0748  0.0000  0.1583  0.3305  0.8550  0.0000  0.0000\n",
      "  0.0000  0.0229  0.2733  0.0000  0.2127  1.0264  0.0914  0.0000\n",
      "  0.0000  0.0000  0.1456  0.0000  0.1437  0.1995  0.2885  0.0000\n",
      "[torch.FloatTensor of size 2x5x8]\n",
      "\n",
      "--------------test_hn--------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.0000  0.4289  0.0619  0.0000  0.5793  0.0000  0.5234  0.0000\n",
      "  0.0000  0.0000  0.1456  0.0000  0.1437  0.1995  0.2885  0.0000\n",
      "[torch.FloatTensor of size 1x2x8]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "test_rnn = RNNs.RNN(4, 8, 1)\n",
    "test_input = Variable(torch.randn(2, 5, 4))\n",
    "# test_h0 = Variable(torch.randn(1, 8))\n",
    "test_output, test_hn = test_rnn(input)\n",
    "print(\"------------test_input------------\")\n",
    "print(test_input)\n",
    "print(\"------------test_output------------\")\n",
    "print(test_output)\n",
    "print(\"--------------test_hn--------------\")\n",
    "print(test_hn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------input------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.5762  0.2814  1.6233 -1.1057\n",
      " -0.6997 -0.1917  0.2144  1.5355\n",
      " -1.2808  0.2099 -0.4571 -0.0492\n",
      " -0.7842  2.2718 -0.6413 -0.2596\n",
      " -0.3344  0.5711 -0.1468  0.9883\n",
      "[torch.FloatTensor of size 1x5x4]\n",
      "\n",
      "-------------h0--------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -1.3383  0.0800  0.2531 -1.0310  0.8557 -1.6803  0.2802 -0.3195\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      "\n",
      "-------------c0--------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -3.0398  0.2847  0.0945 -0.9182  0.7930 -0.2353 -0.4862  0.4459\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      "\n",
      "-----------output------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.3385 -0.1427 -0.0373 -0.0938  0.3646  0.1798 -0.1613 -0.0121\n",
      " -0.4115 -0.0626  0.0333  0.0600  0.1474  0.1704  0.1491  0.0637\n",
      " -0.1575 -0.0579 -0.0438  0.1106  0.0865  0.1315  0.1470 -0.0053\n",
      " -0.1306  0.0020 -0.0139  0.0458 -0.1078 -0.0378  0.0875  0.0531\n",
      "  0.0078 -0.0044  0.0059  0.0897 -0.1912  0.0185  0.1318  0.1016\n",
      "[torch.FloatTensor of size 1x5x8]\n",
      "\n",
      "-------------hn--------------\n",
      "(Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.0078 -0.0044  0.0059  0.0897 -0.1912  0.0185  0.1318  0.1016\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      ", Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.0128 -0.0094  0.0111  0.1756 -0.3989  0.0462  0.2257  0.2505\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "lstm = nn.LSTM(4, 8, 1, batch_first=True)\n",
    "input = Variable(torch.randn(1, 5, 4))\n",
    "h0 = Variable(torch.randn(1, 1, 8))\n",
    "c0 = Variable(torch.randn(1, 1, 8))\n",
    "output, hn = lstm(input, (h0, c0))\n",
    "print(\"------------input------------\")\n",
    "print(input)\n",
    "print(\"-------------h0--------------\")\n",
    "print(h0)\n",
    "print(\"-------------c0--------------\")\n",
    "print(c0)\n",
    "print(\"-----------output------------\")\n",
    "print(output)\n",
    "print(\"-------------hn--------------\")\n",
    "print(hn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------test_input------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -1.3387 -0.4349  0.2622 -0.7650\n",
      "  0.4395  0.0923  0.2480 -1.5539\n",
      " -0.7405  1.3953 -0.3189  2.8691\n",
      " -0.0222  0.9492  1.3573 -1.4331\n",
      "  2.5375 -0.0272 -0.1814  0.7661\n",
      "[torch.FloatTensor of size 1x5x4]\n",
      "\n",
      "------------test_output------------\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      " -0.0952 -0.0180 -0.1259 -0.0743 -0.1737  0.0592 -0.0302 -0.0129\n",
      " -0.1220 -0.0021 -0.1578 -0.0795 -0.0992  0.1240  0.0533  0.0520\n",
      "  0.1552  0.2437  0.0225  0.2073 -0.0328 -0.1366 -0.0494  0.0125\n",
      "  0.0030  0.1355 -0.0753  0.1013 -0.0807 -0.1147 -0.0199  0.0412\n",
      "  0.1508  0.2097 -0.0560  0.1023 -0.0684 -0.2219  0.1918  0.3143\n",
      "[torch.FloatTensor of size 1x5x8]\n",
      "\n",
      "--------------test_hn------------\n",
      "(Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.1508  0.2097 -0.0560  0.1023 -0.0684 -0.2219  0.1918  0.3143\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      ", Variable containing:\n",
      "(0 ,.,.) = \n",
      "  0.2589  0.3374 -0.1806  0.2377 -0.1273 -0.5126  0.4006  0.6366\n",
      "[torch.FloatTensor of size 1x1x8]\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "test_lstm = RNNs.LSTM(4, 8, 1)\n",
    "test_input = Variable(torch.randn(1, 5, 4))\n",
    "# test_h0 = Variable(torch.randn(1, 8))\n",
    "test_output, test_hn = test_lstm(test_input)\n",
    "print(\"------------test_input------------\")\n",
    "print(test_input)\n",
    "print(\"------------test_output------------\")\n",
    "print(test_output)\n",
    "print(\"--------------test_hn------------\")\n",
    "print(test_hn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
